package com.japp.sys.service;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.japp.base.BaseService;
import com.japp.sys.bean.ElementBean;
import com.japp.sys.mapper.ElementMapper;
import com.japp.tools.common.JappLog;

/**
 * 
 * @项目名称:中钢期货
 * @类名: ElementService 
 * @说明: 元素信息Service层
 * @作者: Jion
 * @时间: 2013-5-22 下午8:31:14 
 * @版权所属:博思恒信科技有限公司
 * @版本 V1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ElementService implements BaseService {

	@Autowired
	private ElementMapper element;
	
	public String queryElementAll(int roleId){
		List<ElementBean> list = null;
		try {
			list = element.queryElementAll(roleId);
		} catch (Exception e) {
			JappLog.log.error("",e);
		}
		if(list==null||list.size()==0)return "";
		String webctx = ServletActionContext.getRequest().getContextPath();
		String json = "[";
		for (ElementBean e:list) {
			json+="{";
			if(e.getEm_id()!=0){
				json+="id:"+e.getEm_id()+",";
			}
			json+="parentId:"+e.getEm_pid()+",";
			String name = e.getEm_name();
			if(name!=null&&!"".equals(name)){
				json+="name:'"+name+"',";
			}
			String em_target = e.getEm_target();
			if(em_target!=null&&!"".equals(em_target)){
				json+="target:'"+em_target+"',";
			}
			String em_url = e.getEm_url();
			if(em_url!=null&&!"".equals(em_url)){
				json+="url:'"+webctx+"/"+em_url+"',";
			}
			String em_attribute = e.getEm_attribute();
			if(em_attribute!=null&&!"".equals(em_attribute)){
				for(String s:em_attribute.split(",")){
					json+=s+",";
				}
			}
			json = json.substring(0,json.length()-1);
			json+="},";
		}
		json = json.substring(0,json.length()-1);
		json+="];";
		return json;
	}
	
	public String queryElementAllToleft(int roleId){
		List<ElementBean> list = null;
		try {
			list = element.queryElementAll(roleId);
		} catch (Exception e) {
			JappLog.log.error("",e);
		}
		if(list==null||list.size()==0)return "";
		String webctx = ServletActionContext.getRequest().getContextPath();
		String json = "[";
		for (ElementBean e:list) {
			json+="{";
			if(e.getEm_id()!=0){
				json+="id:"+e.getEm_id()+",";
			}
			json+="parentId:"+e.getEm_pid()+",";
			String name = e.getEm_name();
			if(name!=null&&!"".equals(name)){
				json+="name:'"+name+"',";
			}
			String em_target = e.getEm_target();
			if(em_target!=null&&!"".equals(em_target)){
				json+="target:'"+em_target+"',";
			}
			String em_url = e.getEm_url();
			if(em_url!=null&&!"".equals(em_url)){
				json+="urls:'"+webctx+"/"+em_url+"',";
			}
			String em_attribute = e.getEm_attribute();
			if(em_attribute!=null&&!"".equals(em_attribute)){
				for(String s:em_attribute.split(",")){
					json+=s+",";
				}
			}
			json = json.substring(0,json.length()-1);
			json+="},";
		}
		json = json.substring(0,json.length()-1);
		json+="];";
		return json;
	}
	
	public String queryElementAllJson(int roleId){
		List<ElementBean> list = element.queryElementAll(roleId);
		String json = "[";
		for (ElementBean e:list) {
			json+="{";
			if(e.getEm_id()!=0){
				json+="id:'"+e.getEm_id()+"',";
			}
			json+="parentId:'"+e.getEm_pid()+"',";
			String name = e.getEm_name();
			if(name!=null&&!"".equals(name)){
				json+="name:'"+name+"',";
			}
			String em_attribute = e.getEm_attribute();
			if(em_attribute!=null&&!"".equals(em_attribute)){
				for(String s:em_attribute.split(",")){
					json+=s+",";
				}
			}
			json = json.substring(0,json.length()-1);
			json+="},";
		}
		json = json.substring(0,json.length()-1);
		json+="];";
		return json;
	}	
	
}
